<?php

namespace catchAdmin\drug\service;

use MongoDB\BSON\ObjectId;
use catcher\exceptions\FailedException;
use app\common\model\MedicationReminder as ReminderModel;

class MedicationReminderService
{
    /**
     * 保存用药提醒
     * @param array $param
     * @retrun bool
     */
    public function saveMedicationReminder(array $param): bool
    {
        try {
            $userInfo = request()->user();
            $userDept = $userInfo->userDept??[];

            foreach ($param['reminderList'] as $key => $val) {

                //验证用药周期
                if ($val['cycle_unit'] == 1 && empty($val['cycle'])) {
                    throw new FailedException('用药周期不可为空');
                }

                //检查用药周期和提醒频次是否匹配
                $this->checkMateRemind((int)$val['frequency'], (int)$val['cycle_unit'], (int)$val['cycle']??0);

                //检查首次提醒时间
                if (strtotime($val['first_reminder_time']) <= strtotime(date("Y-m-d"))) {
                    throw new FailedException("首次提醒时间必须大于今天");
                }

                //生成主键
                $primary_key = (string)new ObjectId();
                $param['reminderList'][$key]['_id'] = $primary_key;
                $param['reminderList'][$key]['creator_id'] = $param['creator_id'];
                $param['reminderList'][$key]['creator_name'] = $userInfo['username'];
                $param['reminderList'][$key]['department_id'] = $userInfo['department_id'];
                $param['reminderList'][$key]['department_name'] = $userDept['department_name']??'';
                $param['reminderList'][$key]['deleted_at'] = 0;

                //重组服用时间
                $param['reminderList'][$key]['take_time'] = [];
                foreach ($val['take_time'] as $k => $v) {
                    $param['reminderList'][$key]['take_time'][] = [
                        'take_time_id' => (string)new ObjectId(),
                        'take_time_value' => $v['take_time_value']
                    ];
                }
            }
            (new ReminderModel())->saveAll($param['reminderList']);
            return true;
        } catch (\Exception $exception) {
            throw new FailedException($exception->getMessage());
        }
    }

    /**
     * 检查用药周期和提醒频次是否匹配
     * @param int $frequency 提醒频次
     * @param int $cycle_unit 用药周期单位
     * @param int $cycle 用药周期
     * @return void
     */
    private function checkMateRemind(int $frequency, int $cycle_unit, int $cycle=0): void
    {
        //用药周期是天
        if ($cycle_unit == 1) {
            switch ($frequency) {
                case 1:
                    if ($cycle < 1) {
                        throw new FailedException("用药周期不可小于1天");
                    }
                    break;
                case 2:
                    if ($cycle < 8) {
                        throw new FailedException("用药周期不可小于8天");
                    }
                    break;
                case 3:
                    if ($cycle < 30) {
                        throw new FailedException("用药周期不可小于30天");
                    }
                    break;
                default:
                    throw new FailedException("提醒频次错误");
            }
        }
    }
}